package servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class test extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		if (ServletFileUpload.isMultipartContent(request)) {  
			  
	        try {  
	        // 1. 创建DiskFileItemFactory对象，设置缓冲区大小和临时文件目录  
	        DiskFileItemFactory factory = new DiskFileItemFactory();  
	        // System.out.println(System.getProperty("java.io.tmpdir"));//默认临时文件夹  
	  
	        // 2. 创建ServletFileUpload对象，并设置上传文件的大小限制。  
	        ServletFileUpload sfu = new ServletFileUpload(factory);  
	        //限制图片大小为2Mb
	        sfu.setSizeMax(10 * 1024*1024 );// 以byte为单位 不能超过10M 1024byte =  
	                         // 1kb 1024kb=1M 1024M = 1G  
	        sfu.setHeaderEncoding("utf-8");  
	  
	        // 3.  
	        // 调用ServletFileUpload.parseRequest方法解析request对象，得到一个保存了所有上传内容的List对象。  
	        List<FileItem> fileItemList = sfu.parseRequest(request);  
	        Iterator<FileItem> fileItems = fileItemList.iterator();  
	  
	        // 4. 遍历list，每迭代一个FileItem对象，调用其isFormField方法判断是否是上传文件  
	        while (fileItems.hasNext()) {  
	            FileItem fileItem = fileItems.next();  
	            // 普通表单元素  
	            if (fileItem.isFormField()) {  
	            String name = fileItem.getFieldName();// name属性值  
	            String value = fileItem.getString("utf-8");// name对应的value值  
	            if (name.equals("user_date")) {
					String  vString=value.replaceAll("T", " ")+":00";
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟  
					java.util.Date date=sdf.parse(vString);
				}
//	            System.out.println(name + " = " + value);  
	            }  
	        }
	        }catch (FileUploadException e) {  
		        e.printStackTrace();  
		        } catch (Exception e) {  
		        e.printStackTrace();  
		        }  
	        }
		}
	}
	


